<template>
  <div class="f-echarts" :style="{'height':height + 'px', 'width':width}"></div>
</template>
<script>
import ECharts from 'echarts'
import 'echarts/theme/macarons' // theme 主题
import { EchartFormate } from '@/utils/echarts-formate'
import FSesize from './f-resize'
export default {
  name: 'FPie',
  mixins: [FSesize],
  components: {
    ECharts
  },
  data () {
    return {
      width: '100%',
      chart: null
    }
  },
  props: {
    height: {
      type: Number,
      default: 200
    },
    title: {
      type: String,
      default: ''
    },
    subText: {
      type: String,
      default: ''
    },
    chartData: {
      type: Array,
      required: true
    },
    roseType: {
      type: Boolean,
      default: false
    },
    radius: {
      type: Boolean,
      default: false
    },
    showTitle: {
      type: Boolean,
      default: true
    },
    showLegend: {
      type: Boolean,
      default: true
    }
  },
  mounted () {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy () {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart () {
      this.chart = ECharts.init(this.$el, 'macarons')
      this.setOptions()
    },
    setOptions () {
      this.chart.showLoading()
      const datas = EchartFormate.NoGroupFormate(this.chartData)
      this.chart.hideLoading()
      const option = {
        tooltip: {
          show: true,
          trigger: 'item',
          formatter: function (item) {
            return item.seriesName + '</br>' + item.marker + item.name + ': ' + item.value + ' (' + item.percent + '%)'
          }
        },

        series: [
          {
            name: this.title,
            type: 'pie',
            radius: [0, 90],
            selectedMode: 'single',
            center: ['50%', '50%'],
            data: datas.data,
            animationEasing: 'cubicInOut',
            animationDuration: 2600
          }
        ]
      }
      if (this.roseType) {
        option.series[0].roseType = 'radius'
      }
      if (this.radius) {
        option.series[0].radius = [15, 90]
      }
      if (this.showTitle) {
        option.title = {
          text: this.title,
          subtext: this.subText,
          x: 'center'
        }
      }
      if (this.showLegend) {
        option.legend = {
          // 组建
          orient: 'horizontal', // 垂直：vertical； 水平 horizontal  top: 'center',位置默认左
          left: 'center', // 位置默认右
          top: this.showTitle ? 'bottom' : 'top',
          data: datas.categorys
        }
      }
      this.chart.setOption(option, true)
    }
  }
}
</script>
<style scoped>
  .f-echarts{
    background-color: #FFFFFF;
  }
</style>
